//
//  MSFightHeroInfoNode.m
//  MagicSword
//
//  Created by Netease on 15/11/30.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSFightHeroInfoNode.h"
#import "MSWeaponNode.h"
#import "MSProgressBar.h"
#import "MSGameManager.h"
#import "MSUpArrowSprite.h"

@implementation MSFightHeroInfoNode {
    SKSpriteNode *_closeSprite;
    SKLabelNode  *_expLabel;
    MSProgressBar *_expProgressBar;
    NSMutableArray *_upSpriteArray;
}

- (id)init {
    self = [super init];
    if (self) {
        [self setDoneButtonHidden:YES];
        _upSpriteArray = [NSMutableArray array];
        
        _closeSprite = [SKSpriteNode spriteNodeWithImageNamed:@"main_alert_close"];
        _closeSprite.zPosition = 3;
        [_closeSprite setSize:CGSizeMake(30, 30)];
        [_closeSprite setPosition:CGPointMake(CGRectGetWidth(self.frame)/2-30, CGRectGetHeight(self.frame)/2-30)];
        _closeSprite.name = NodeName_CloseHeroInfoButtonNode;
        [self addChild:_closeSprite];
        
        _expLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        [_expLabel setText:@"经验："];
        _expLabel.zPosition = 3;
        [_expLabel setPosition:CGPointMake(-65, -90)];
        [_expLabel setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeLeft];
        [_expLabel setFontSize:12];
        [_expLabel setFontColor:[UIColor whiteColor]];
        [self addChild:_expLabel];
        
        _expProgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(100, 20)];
        _expProgressBar.zPosition = 3;
        [_expProgressBar setPosition:CGPointMake(20, -85)];
        [self addChild:_expProgressBar];
    }
    return self;
}

- (void)setHeroInfo:(MSHeroInfo *)heroInfo {
    [super setHeroInfo:heroInfo];
    
    [self setHeroWeaponInfo:heroInfo.weaponList];
    [self setProgressBar:heroInfo.HP m:heroInfo.MaxHP isBlood:YES];
    
    NSInteger nextExp = [[MSGameManager shareManager] getLevelNeedExperience:heroInfo.level];
    NSString *string = [NSString stringWithFormat:@"%d/%d", heroInfo.Experience, nextExp];
    [_expProgressBar.titleLabelNode setText:string];
    [_expProgressBar setProgress:heroInfo.Experience/(float)nextExp isBlood:NO];
}

- (void)setHeroWeaponInfo:(NSArray *)weaponArray {
    for (int i=0; i<3; i++) {
        MSWeaponNode *weaponSpite = nil;
        if (weaponArray.count > i) {
            weaponSpite = [[MSWeaponNode alloc] initWithWeaponInfo:weaponArray[i]];
        } else {
            weaponSpite = [[MSWeaponNode alloc] init];
        }
        
        weaponSpite.position = CGPointMake(-50+i*50, -CGRectGetHeight(self.frame)/2+30);
        weaponSpite.size = CGSizeMake(30, 30);
        weaponSpite.zPosition = 12;
        [weaponSpite setShapeNodeHidden:NO];
        weaponSpite.name = NodeName_AttackLookNode;
        [self addChild:weaponSpite];
    }
}

- (void)showWithAnimation {
    [self setHidden:NO];
    self.position = CGPointMake(-CGRectGetWidth(self.frame)/2, self.position.y);
    [self runAction:[SKAction moveToX:CGRectGetWidth(self.frame)/2 duration:0.2]];
}

- (void)hideWithAnimation {
    [self runAction:[SKAction moveToX:-CGRectGetWidth(self.frame)/2 duration:0.2] completion:^{
        [self setHidden:YES];
    }];
    
    [_upSpriteArray makeObjectsPerformSelector:@selector(removeFromParent)];
    [_upSpriteArray removeAllObjects];
}

- (void)showLevelUp:(MSLevelUpInfo *)info {
    float left = 40;
    float top = 23;
    if (info.MaxHP > 0) {
        [self createUpArrowSprite:CGPointMake(left, top) sting:[NSString stringWithFormat:@"+%d", info.MaxHP]];
    }
    if (info.Attack > 0) {
        [self createUpArrowSprite:CGPointMake(left, top-20) sting:[NSString stringWithFormat:@"+%d", info.Attack]];
    }
    if (info.DoubleHit > 0) {
        [self createUpArrowSprite:CGPointMake(left, top-40) sting:[NSString stringWithFormat:@"+%d%%", info.DoubleHit]];
    }
    if (info.Vampire > 0) {
        [self createUpArrowSprite:CGPointMake(left, top-60) sting:[NSString stringWithFormat:@"+%d%%", info.Vampire]];
    }
    if (info.MissValue > 0) {
        [self createUpArrowSprite:CGPointMake(left, top-80) sting:[NSString stringWithFormat:@"+%d%%", info.MissValue]];
    }
}

- (void)createUpArrowSprite:(CGPoint)point sting:(NSString *)string {
    MSUpArrowSprite *sprite = [[MSUpArrowSprite alloc] init];
    sprite.zPosition = 12;
    sprite.size = CGSizeMake(15, 15);
    sprite.position = point;
    [sprite runAction];
    [self addChild:sprite];
    
    SKLabelNode  *label = [[SKLabelNode alloc] initWithFontNamed:@"Chalkduster"];
    [label setText:string];
    [label setZPosition:12];
    [label setPosition:CGPointMake(point.x+8, point.y-2)];
    [label setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeLeft];
    [label setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];
    [label setFontSize:13];
    [label setFontColor:[UIColor greenColor]];
    [self addChild:label];
    
    [_upSpriteArray addObject:sprite];
    [_upSpriteArray addObject:label];
}

@end
